<?php
##############################################
#
#   活动订单表
# @ Time : 2017-2-28 - 下午11:39:25
# @ Author : JiangYongWei <1085562810@qq.com>
# @ Encoding : UTF-8 language : PHP
#
##############################################

namespace Home\Model;

class ActivityOrderModel extends IndexBaseModel{

	/*活动订单表  */
	protected $tableName = 'activity_order';
	private $_field = array('r.id', 'r.order_sn', 'r.activity_id', 'r.activity_name', 'r.amount',
                	        'r.order_status', 'r.payment_status', 'ac.sponsor', 'ac.area', 
                	        'ac.activity_img', 'ac.signup_number','ac.price',
	                        'ac.start_time','ac.end_time'
	    );
	
	/**
	 * 提交订单
	 */
	public function addOrder($data) {
	    return $this->add($data);
	}
	
	/**
	  * 统计订单
	  * @param int $activity_id
	  * @param int $user_id
	  * @date 2017年8月7日 - 下午2:26:43
	  * @author JiangYongWei
	 */
	public function order_count($user_id) {
	    $map = "user_id = $user_id";
        return  array(
	        $this->where($map." and order_status = 1 and payment_status = 1")->count(),
	        $this->where($map." and order_status = 2 and payment_status = 2")->count(),
	        $this->where($map." and order_status = 3 and payment_status = 2")->count(),
	        $this->where($map." and order_status = 4")->count()
	    );
	}
	
	/**
	  * 全部订单
	  * @param int $user_id
	  * @date 2017年9月1日 - 上午10:56:21
	  * @author JiangYongWei
	 */
	public function allOrder($user_id) {
	    $map = array('r.user_id'=>$user_id);// 'end_time'=>array('egt', time())
	    $count = $this->alias('r')->join("left join __ACTIVITY__ ac on ac.id = r.activity_id")
	                  ->where($map)->count();// 查询满足要求的总记录数
	    $Page = new \Think\Page($count, 10);
	    $show = $Page->show();
	    $result = $this->alias('r')
	                   ->field($this->_field)
	                   ->join("left join __ACTIVITY__ ac on ac.id = r.activity_id")
	                   ->where($map)
	                   ->order('id desc')
	                   ->limit($Page->firstRow.','.$Page->listRows)
	                   ->select();
	    return array($result, $show, $count);
	}
	
	/**
	 * 订单
	 * @param int $user_id
	 * @date 2017年9月1日 - 上午10:56:21
	 * @author JiangYongWei
	 */
	public function orderStatus($user_id, $order_status) {
	    $map = array('r.user_id'=>$user_id, 'r.order_status'=>$order_status);// 'end_time'=>array('egt', time())
	    $count = $this->alias('r')->join("left join __ACTIVITY__ ac on ac.id = r.activity_id")
	    ->where($map)->count();// 查询满足要求的总记录数
	    $Page = new \Think\Page($count, 10);
	    $show = $Page->show();
	    $result = $this->alias('r')
                	    ->field($this->_field)
                	    ->join("left join __ACTIVITY__ ac on ac.id = r.activity_id")
                	    ->where($map)
                	    ->order('id desc')
                	    ->limit($Page->firstRow.','.$Page->listRows)
                	    ->select();
	    return array($result, $show);
	}
	
	/**
	  * 订单详情
	  * @param int $order_id
	  * @date 2017年9月4日 - 下午3:18:18
	  * @author JiangYongWei
	 */
	public function orderDetail($order_id){
	    array_push($this->_field,'ac.detail_address','ac.contact_mobile','ac.describe',
	               'ac.share_number','ac.browse_number',
	               'r.name','r.mobile','r.email'
	        );
	    $data = $this->alias('r')->field($this->_field)
	                 ->join("left join __ACTIVITY__ ac on ac.id = r.activity_id")
	                 ->where("r.id = $order_id")
	                 ->find();
	    return $data;
	}
	
	/**
	  * 查看电子票
	  * @param int $order_id
	  * @date 2017年9月5日 - 下午4:52:18
	  * @author JiangYongWei
	 */
	public function getElectronics($order_id){
	    array_push($this->_field,'ac.detail_address','ac.contact_mobile','r.user_id','r.sponsor_user_id');
	    $data = $this->alias('r')->field($this->_field)
	                 ->join("left join __ACTIVITY__ ac on ac.id = r.activity_id")
	                 ->where("r.id = $order_id")
	                 ->find();
	    return $data;
	}
	
	/**
	  * 获取当前扫码的订单状态
	  * @param string $order_sn
	  * @date 2017年9月12日 - 下午2:38:30
	  * @author JiangYongWei
	 */
	public function getOrderStatus($order_sn){
    	return $this->where("order_sn = '$order_sn'")->getField('order_status');
	}
	
	/**
	  * 验票修改订单已完成状态
	  * @param int $user_id
	  * @param string $order_sn
	  * @date 2017年9月12日 - 上午11:22:26
	  * @author JiangYongWei
	 */
	public function checkTicket($order_sn) {
	    $data = array('order_status'=>3);
	    return $this->where("order_sn = '{$order_sn}'")->save($data);
	}
}
